package com.wires.lu.practice;


import com.sun.javafx.util.WeakReferenceQueue;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * 引用队列
 *
 * 引用被回收之前会放到引用对列中保存
 */
public class RefereceQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        Object obj=new Object();
        ReferenceQueue<Object> referenceQueue=new ReferenceQueue<>();
        WeakReference<Object> weakReference=new WeakReference<>(obj,referenceQueue);
        System.out.println(obj);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());
        System.out.println("=========================");
        obj=null;
        System.gc();
        Thread.sleep(500);
        System.out.println(obj);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());
    }

}
